package com.eonmind.vip.mapper;

import cn.hutool.core.util.ObjectUtil;
import com.eonmind.common.contract.data.base.BaseMongoMapper;
import com.eonmind.common.provider.entity.vip.UserWelfare;
import com.mongoplus.conditions.query.QueryWrapper;
import com.mongoplus.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class UserWelfareMapper extends BaseMongoMapper<UserWelfare> {

    public UserWelfare getUserPlanByUserID(Long userId) {
        QueryWrapper<UserWelfare> queryWrapper = createMgoQuery().eq(UserWelfare::getUserId, userId);
        UserWelfare userWelfare = one(queryWrapper);
        return userWelfare;

    }

    public Boolean updateUserPlanByUserID(Long userId, UserWelfare userWelfare) {
        UpdateWrapper<UserWelfare> updateWrapper = createMgoUpdate().eq(UserWelfare::getUserId, userId)
                .set(ObjectUtil.isNotEmpty(userWelfare.getUpdateTime()), UserWelfare::getUpdateTime, userWelfare.getUpdateTime())
                .set(ObjectUtil.isNotEmpty(userWelfare.getActiveSubscriptionIds()), UserWelfare::getActiveSubscriptionIds, userWelfare.getActiveSubscriptionIds())
                .set(ObjectUtil.isNotEmpty(userWelfare.getSubscriptionHistoryIds()), UserWelfare::getSubscriptionHistoryIds, userWelfare.getSubscriptionHistoryIds())
                .set(ObjectUtil.isNotEmpty(userWelfare.getCachedFeatures().getTotalCoins()), "cachedFeatures.totalCoins", userWelfare.getCachedFeatures().getTotalCoins())
                .set(ObjectUtil.isNotEmpty(userWelfare.getCachedFeatures().getDailyCoins()), "cachedFeatures.dailyCoins", userWelfare.getCachedFeatures().getDailyCoins())
                .set(ObjectUtil.isNotEmpty(userWelfare.getCachedFeatures().getAllCoins()), "cachedFeatures.allCoins", userWelfare.getCachedFeatures().getAllCoins())
                .set(ObjectUtil.isNotEmpty(userWelfare.getCachedFeatures().getUsedCoins()), "cachedFeatures.usedCoins", userWelfare.getCachedFeatures().getUsedCoins())
                .set(ObjectUtil.isNotEmpty(userWelfare.getCachedFeatures().getFeatures()) && ObjectUtil.isNotEmpty(userWelfare.getCachedFeatures().getFeatures().getChat()), "cachedFeatures.features.chat", userWelfare.getCachedFeatures().getFeatures().getChat())
                .set(ObjectUtil.isNotEmpty(userWelfare.getCachedFeatures().getFeatures()) && ObjectUtil.isNotEmpty(userWelfare.getCachedFeatures().getFeatures().getDrawing()), "cachedFeatures.features.drawing", userWelfare.getCachedFeatures().getFeatures().getDrawing())
                .set(ObjectUtil.isNotEmpty(userWelfare.getCachedFeatures().getFeatures()) && ObjectUtil.isNotEmpty(userWelfare.getCachedFeatures().getFeatures().getVideo()), "cachedFeatures.features.video", userWelfare.getCachedFeatures().getFeatures().getVideo())
                .set(ObjectUtil.isNotEmpty(userWelfare.getDel()), UserWelfare::getDel, userWelfare.getDel());
        Boolean res = update(updateWrapper);
        return res;
    }

    public Boolean updateTotalPoints(Long userId, Integer addPoints) {
        UpdateWrapper<UserWelfare> updateWrapper = createMgoUpdate()
                .eq(UserWelfare::getUserId, userId)
                .inc("cachedFeatures.totalCoins", addPoints);
        return update(updateWrapper);
    }


}
